﻿
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Web.Mvc;
using Peak.ApplicationServices;
using Peak.ApplicationServices.Services;
using Peak.Core;
using Peak.Core.DataInterfaces;
using Peak.Web.Controllers.Models;
using Peak.Web.Controllers.Services;
using SharpArch.Core.PersistenceSupport;
using SharpArch.Data.NHibernate;
using SharpArch.Web.NHibernate;
using MvcContrib;
using Peak.Resources;
using IStoryService = Peak.ApplicationServices.Services.IStoryService;

namespace Peak.Web.Controllers
{
  [HandleError]
  public class StoryController : BaseController
  {
    private readonly IStoryService _storyService;
    private readonly ISprintStoryService _sprintStoryService;

    public StoryController(ISessionService sessionService, IStoryService storyService, ISprintStoryService sprintStoryService)
      : base(sessionService)
    {
      _storyService = storyService;
      _sprintStoryService = sprintStoryService;
    }

    public ActionResult Index(int sprintStoryId, TaskFilterModel taskFilter)
    {
      SprintStory sprintStory = _sprintStoryService.Get(sprintStoryId);      
      return View(new StoryPreviewModel(sprintStory, taskFilter, _sessionService.Project.Users));
    }

    public ActionResult New()
    {
      return View(new StoryModel());
    }

    [AcceptVerbs(HttpVerbs.Post)]
    [ValidateInput(false)]
    [Transaction]
    public ActionResult New(StoryModel story)
    {
      if(!story.IsValid())
      {
        return View(story);
      }

      Story dbStory = new Story();
      story.Update(dbStory);
      dbStory.Created = DateTime.Now;
      dbStory.CreatedBy = Thread.CurrentPrincipal.Identity.Name;
      _sessionService.Project.AddStory(dbStory);

      return Back();
    }

    public ActionResult Edit(int storyId)
    {
      Story story = _storyService.Get(storyId);
      return View(new StoryModel(story));
    }

    [AcceptVerbs(HttpVerbs.Post)]
    [ValidateInput(false)]
    [Transaction]
    public ActionResult Edit(StoryModel story)
    {
      if (!story.IsValid())
      {
        return View(story);
      }

      Story dbstory = _storyService.Get(story.StoryId);
      try
      {
        story.Update(dbstory);
      } 
      catch(NotFinishedTasksOrBugsException)
      {
        ModelState.AddModelError<StoryModel>(f => f.Status, ValidationErrors.HasNotFinishedTasksOrBugs);
        NHibernateSession.Current.Transaction.Rollback();
        return View(story);
      }            

      return Back();
    }

    [AcceptVerbs(HttpVerbs.Post)]
    [ValidateInput(false)]
    [Transaction]
    public ActionResult EditStoryPoints(int id, double value)
    {
      Story dbstory = _storyService.Get(id);
      dbstory.StoryPoints = value;
      _storyService.Update(dbstory);

      return  this.Content(value.ToString());
    }

    [AcceptVerbs(HttpVerbs.Post)]
    [ValidateInput(false)]
    [Transaction]
    public ActionResult EditPriority(int id, int value)
    {
      Story dbstory = _storyService.Get(id);
      dbstory.Priority = value;
      _storyService.Update(dbstory);

      return this.Content(value.ToString());
    }   
  }
}
